"""
从最开始的dcm 到 被模型训练的png 的全套操作流程
1. dcm分类（可选）：2015-16需要先根据series_number和series_description划分序列，2022HCC已经被分好了所以不需要该步骤
2. dcm to nii:：要从dcm里获取study ID 作为后续png文件的命名
2. (原4)nii resize：(目前合入2功能里面)nii的每一帧长宽不一，需要补黑边变成成边长为32*n的正方形(常见的为320*320和256*256)
3. 人为筛选有效的 NC/A/V/D时期 nii文件，请参考相关文档
4. ？？？（未做）：调整图像的亮度、对比度等，使待分割区域明显可见
5. nii to png：格式转换，将每一帧转换成png，限制大小为 512*512，文件名取 nii.studyID - NC/A/V/D - Number
"""
import os
from utils.dcm2nii import dicom_series_to_nifti
from utils.niiProcess import delete_unused_nii_files, rename_nii_files_by_phases, nii_resize

if __name__ == "__main__":
    # 以2022为例
    all_path = r"C:\Users\18049\Desktop\nii"
    dcm_path_2022 = all_path + r"\1_unclassified_dcm\metastasis 2022"
    nii_path_2022 = all_path + r"\2_classification_nii\metastasis 2022"
    nii_path_2022_resized = all_path + r"\2_classification_nii\metastasis 2022 resized"
    # 1. 不需要操作
    # 2. dcm to nii
    # 目标结构：metastasis 2022\XXX_M000xxxxx\MR__20XX0X0Xxxxxxx_1XXXX2\15_t1_vibe_fs_tra_bh_p2_two scans_2XXX8
    for patient_dcm_folder in os.listdir(dcm_path_2022):  # 遍历根文件夹下的所有子文件夹
        patient_dcm_folder_path = os.path.join(dcm_path_2022, patient_dcm_folder)  # C:\..\CHEN YI JIANG_M00xxxxx
        if os.path.isdir(patient_dcm_folder_path):  # 检查是否是文件夹
            print('-' * 50, '\n', patient_dcm_folder, sep='')
            patient_nii_folder_path = os.path.join(nii_path_2022, patient_dcm_folder)  # 创建存放目标nii的文件夹
            if not os.path.exists(patient_nii_folder_path):
                os.makedirs(patient_nii_folder_path)
            mr_temp_folder = os.listdir(patient_dcm_folder_path)[0]
            mr_temp_folder_path = os.path.join(patient_dcm_folder_path, mr_temp_folder)  # C:\..\MR__20220xxxx55_19xxxxx
            for phases_folder in os.listdir(mr_temp_folder_path):
                phases_folder_path = os.path.join(mr_temp_folder_path, phases_folder)
                # print(phases_folder_path)
                try:
                    dicom_series_to_nifti(phases_folder_path, patient_nii_folder_path)
                except Exception as e:
                    # 处理异常，例如输出错误信息
                    print(f"Error processing {phases_folder}: {str(e)}")
                    # 继续循环到下一个对象
                    continue

    # 3. 筛选文件
    # 删除无关nii
    for patient_dcm_folder in os.listdir(nii_path_2022):
        patient_dcm_folder_path = os.path.join(nii_path_2022, patient_dcm_folder)  # C:\..\CHEN YI JIANG_M00xxxxx
        if os.path.isdir(patient_dcm_folder_path):  # 检查是否是文件夹
            print('-' * 50, '\n', patient_dcm_folder, sep='')
            patient_nii_folder_path = os.path.join(nii_path_2022, patient_dcm_folder)  # 创建存放目标nii的文件夹
            delete_unused_nii_files(patient_nii_folder_path)

    # Rename
    for patient_dcm_folder in os.listdir(nii_path_2022):
        patient_dcm_folder_path = os.path.join(nii_path_2022, patient_dcm_folder)  # C:\..\CHEN YI JIANG_M00xxxxx
        if os.path.isdir(patient_dcm_folder_path):  # 检查是否是文件夹
            print('-' * 50, '\n', patient_dcm_folder, sep='')
            patient_nii_folder_path = os.path.join(nii_path_2022, patient_dcm_folder)  # 创建存放目标nii的文件夹
            rename_nii_files_by_phases(patient_nii_folder_path)
